package com.daify.threadlearn.chapter01.order03;

import java.util.ArrayList;
import java.util.List;

/**
 * @author daify
 * @date 2019-06-17
 */
public class ErrorThread {

    private static UnsafeSequence obj = null;

    private static UnsafeSequence getObject () {
        if (obj == null) {
            obj = new UnsafeSequence();
        }
        return obj;
    }


    private static int stock = 100;


    private static int sale () {
        if (stock > 0) {
            try {
                // 模拟业务逻辑
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            stock = stock - 1;
            System.out.println(stock);
            return stock;
        }
        return -100;
    }

    public static void main(String[] args) {
        List<Thread> threads = new ArrayList<Thread>();

        for (int i = 0; i < 10; i++) {
            Runnable run = new Runnable() {
                public void run() {
                    while (sale () > 0) {

                    }
                }
            };
            Thread thread = new Thread(run);
            threads.add(thread);
        }

        for (int i = 0; i < 10; i++) {
            threads.get(i).start();
        }
    }




}
